package service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import db.DBConn;
import entities.CartEntity;
import entities.GoodsEntity;
import entities.UserEntity;

public class CollectService {
	// 增加商品至收藏夹
	public static String addItem(String userName, String goodsId)
			throws SQLException {
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		UserEntity user = UserService.getUser(userName);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowTime = sdf.format(new Date());
		String sql = "insert into shop_collect  values( null ,'" + userName
				+ "'," + user.getId() + "," + goodsId + ",'" + nowTime + "');";
		int result = stmt.executeUpdate(sql);
		stmt.close();
		conn.close();
		return result == 0 ? "failed" : "success";
	}

	// 将某个商品移出收藏夹
	public static String deleteItem(String userName, String goodsId)
			throws SQLException {
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();

		String sql = "delete from shop_collect where collect_goodsId = "
				+ goodsId + " and collect_userName like '" + userName + "'";
		int rs = stmt.executeUpdate(sql);
		stmt.close();
		conn.close();
		return rs > 0 ? "success" : "failed";
	}

	// 清空收藏夹
	public static String deleteAll(String userName) throws SQLException {
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();

		String sql = "delete from shop_collect where collect_userName like '"
				+ userName + "'";
		int rs = stmt.executeUpdate(sql);
		stmt.close();
		conn.close();
		return rs > 0 ? "success" : "failed";
	}

	// 检查收藏夹中的是否有某个商品
	public static boolean selectItem(String userName, String goodsId)
			throws SQLException {
		boolean result = false;
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		String sql = "select * from  shop_collect where  collect_userName like '"
				+ userName + "'  and collect_goodsId = " + goodsId;
		ResultSet rs = stmt.executeQuery(sql);
		if (rs.next()) {
			result = true;
		}
		rs.close();
		stmt.close();
		conn.close();
		return result;
	}

	// 获得收藏夹中的所有商品
	public static List<GoodsEntity> selectAll(String userName)
			throws SQLException {
		List<GoodsEntity> goodsList = new ArrayList<GoodsEntity>();
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		String sql = "select * from  shop_collect,shop_goods where  collect_userName like '"
				+ userName + "' and goods_state=1 and goods_id = collect_goodsId";
		ResultSet rs = stmt.executeQuery(sql);
		while (rs.next()) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			GoodsEntity goods = new GoodsEntity();
			goods.setId(rs.getInt("collect_goodsId"));
			goods.setName(rs.getString("goods_name"));
			goods.setPrice(rs.getDouble("goods_price"));
			goods.setImageUrl(rs.getString("goods_image"));
			goods.setOnSaleTime(sdf.format(rs.getTimestamp("collect_time")));

			goodsList.add(goods);
		}
		rs.close();
		stmt.close();
		conn.close();
		return goodsList;
	}

	public static void main(String[] args) throws SQLException {
//		addItem("liwentao", "1");
//		addItem("吟风", "3");
//		addItem("liwentao", "9");
//		addItem("liwentao", "6");
//		addItem("liwentao", "5");
//		addItem("liwentao", "12");
		// addItem("liwentao", "1");
		// deleteItem("liwentao", "1");
		System.out.println(selectItem("liwentao", "1"));
		// deleteAll("liwentao");
		System.out.println(selectAll("liwentao"));
	}
}
